<template>
  <div class="inventory-alerts">
    <h2>库存预警信息</h2>
    <div v-if="alerts.length > 0" class="alert-cards">
      <div v-for="alert in alerts" :key="alert.id" class="alert-card">
        <div class="alert-header">
          <span class="goods-name">{{ alert.goods_name }}</span>
          <span class="alert-status" :class="getAlertStatusClass(alert.current_quantity, alert.min_quantity_alert)"></span>
        </div>
        <div class="alert-body">
          <div class="current-quantity">
            <span>当前库存数量: </span>
            <span :class="getQuantityClass(alert.current_quantity, alert.min_quantity_alert)">{{ alert.current_quantity }}</span>
          </div>
          <div class="min-alert-quantity">
            <span>最小警报数量: </span>
            <span>{{ alert.min_quantity_alert }}</span>
          </div>
          <div class="progress-container">
            <div class="progress-bar">
              <div class="progress" :style="{ width: getProgressPercentage(alert.current_quantity, alert.min_quantity_alert) }"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-else>
      <p>当前没有库存预警信息。</p>
    </div>
  </div>
</template>

<script>
import { getInventoryAlerts } from '@/api/inventory'

export default {
  name: 'InventoryAlerts',
  data() {
    return {
      alerts: []
    };
  },
  created() {
    this.fetchAlerts();
  },
  methods: {
    async fetchAlerts() {
      try {
        const response = await getInventoryAlerts();
        this.alerts = response.data;
      } catch (error) {
        console.error('获取库存预警信息失败:', error);
        this.$message.error('获取库存预警信息失败');
      }
    },
    getProgressPercentage(current, min) {
      const percentage = (current / min) * 100;
      return `${percentage}%`;
    },
    getQuantityClass(current, min) {
      if (current <= min) {
        return 'low-quantity';
      }
      return 'normal-quantity';
    },
    getAlertStatusClass(current, min) {
      if (current <= min) {
        return 'critical-status';
      }
      return 'normal-status';
    }
  }
};
</script>

<style scoped>
.inventory-alerts {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 30px;
}

.alert-cards {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.alert-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
}

.alert-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.goods-name {
  font-weight: bold;
  color: #333;
}

.alert-status {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  display: inline-block;
}

.critical-status {
  background-color: #ff5252;
}

.normal-status {
  background-color: #4caf50;
}

.alert-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.current-quantity, .min-alert-quantity {
  display: flex;
  justify-content: space-between;
}

.low-quantity {
  color: #ff5252;
  font-weight: bold;
}

.normal-quantity {
  color: #4caf50;
  font-weight: bold;
}

.progress-container {
  margin-top: 15px;
}

.progress-bar {
  height: 8px;
  background-color: #e0e0e0;
  border-radius: 4px;
  overflow: hidden;
}

.progress {
  height: 100%;
  background-color: #ff5252;
  transition: width 0.3s ease;
}

.progress.low {
  background-color: #ff5252;
}
</style>